/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Formas;

import datos.*;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

/**
 *
 * @author lupiss
 */
public class AM_registro extends javax.swing.JFrame {
    private FileInputStream fis;
    private int longitudBytes;
    private ControllerCliente cc;
    public tratadatos objDatos=new tratadatos();
    private BDAdmin objCon=new BDAdmin();
    private Cifrador c=new Cifrador(); 
    /**
     * Creates new form AM_registro
     */
    public AM_registro() {
        initComponents();
        this.setLocationRelativeTo(null);
        //System.out.println(ServerBridge.getfechasistema());
        //System.out.println(ServerBridge.getfechasistemacompleta());
        cc = new ControllerCliente();
        jF_fecha.setText(ServerBridge.getfechasistemacompleta());
        jT_uniclave.setText(objDatos.getCadenaAlfanumAleatoria(8));
        lb_foto.setIcon(new CustomImageIcon(getClass().getResource("/resource/defaultlarge.gif")));
        jB_elimina.setVisible(false);
        jB_modifica.setVisible(false);
        //jT_buscanomina.setVisible(false);
        jLabel14.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jB_cancelar = new javax.swing.JButton();
        jB_guarda = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        lb_foto = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jT_nombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jC_posicion = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jC_dpto = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jC_niveleducativo = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jF_fecha = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jT_nunomina = new javax.swing.JTextField();
        jP_contra = new javax.swing.JPasswordField();
        jLabel13 = new javax.swing.JLabel();
        jT_uniclave = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jC_tipo = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTA_RYR = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTA_NYP = new javax.swing.JTextArea();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTA_PYE = new javax.swing.JTextArea();
        jLabel16 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTA_PRIN = new javax.swing.JTextArea();
        jLabel14 = new javax.swing.JLabel();
        jT_buscanomina = new javax.swing.JTextField();
        jB_modifica = new javax.swing.JButton();
        jB_elimina = new javax.swing.JButton();
        jB_limpia = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));

        jB_cancelar.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jB_cancelar.setText("Salir");
        jB_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_cancelarActionPerformed(evt);
            }
        });

        jB_guarda.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jB_guarda.setText("Guardar");
        jB_guarda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_guardaActionPerformed(evt);
            }
        });

        lb_foto.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lb_foto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lb_fotoMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lb_foto, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lb_foto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Century Gothic", 0, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("DATOS GENERALES");

        jLabel2.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel2.setText("Nombre:");

        jT_nombre.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel3.setText("Posicion:");

        jC_posicion.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jC_posicion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Técnico Mecánico", "Op. de Refrigeración", "Técnico de Refrigeración", "Op. de PTAR.", "Jefatura", "Técnico Instrumentista" }));
        jC_posicion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jC_posicionActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel4.setText("Departamento:");

        jC_dpto.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jC_dpto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mantenimiento", "Sala de Maq. de Refrigeración", "PTAR", "Ingenieria de Planta" }));

        jLabel5.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel5.setText("Nivel Educativo:");

        jC_niveleducativo.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jC_niveleducativo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Técnico", "TSU", "Ingeniero" }));

        jLabel6.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel6.setText("Fecha de Ingreso:");

        try {
            jF_fecha.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jF_fecha.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel11.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel11.setText("No. Nomina:");

        jLabel12.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel12.setText("Contraseña:");

        jT_nunomina.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jT_nunomina.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jP_contra.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jP_contra.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel13.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel13.setText("UNICLAVE:");

        jT_uniclave.setEditable(false);
        jT_uniclave.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jT_uniclave.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel15.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel15.setText("Tipo:");

        jC_tipo.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jC_tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Usuario", "Administrador" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jT_nombre)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jC_posicion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jC_dpto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jC_niveleducativo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jF_fecha))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel11)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jT_nunomina))
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel13)
                                            .addComponent(jLabel12)
                                            .addComponent(jLabel15))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jP_contra, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                                            .addComponent(jT_uniclave)
                                            .addComponent(jC_tipo, 0, 1, Short.MAX_VALUE))))))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jT_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jC_posicion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(jT_nunomina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jC_dpto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(jP_contra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jC_niveleducativo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel13)
                    .addComponent(jT_uniclave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jF_fecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15)
                    .addComponent(jC_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(44, Short.MAX_VALUE))
        );

        jLabel7.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("DATOS");

        jLabel8.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel8.setText("Roles y Responsabilidades:");

        jTA_RYR.setColumns(20);
        jTA_RYR.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jTA_RYR.setRows(5);
        jScrollPane1.setViewportView(jTA_RYR);

        jLabel9.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel9.setText("Normas:");

        jTA_NYP.setColumns(20);
        jTA_NYP.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jTA_NYP.setRows(5);
        jScrollPane2.setViewportView(jTA_NYP);

        jLabel10.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel10.setText("Propiedades de Equipo:");

        jTA_PYE.setColumns(20);
        jTA_PYE.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jTA_PYE.setRows(5);
        jScrollPane3.setViewportView(jTA_PYE);

        jLabel16.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel16.setText("Principios:");

        jTA_PRIN.setColumns(20);
        jTA_PRIN.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jTA_PRIN.setRows(5);
        jScrollPane4.setViewportView(jTA_PRIN);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
                            .addComponent(jScrollPane3)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jScrollPane2)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel16)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel9)
                    .addComponent(jLabel16)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(24, 24, 24)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel14.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jLabel14.setText("No Nomina");

        jT_buscanomina.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jT_buscanomina.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jT_buscanominaKeyPressed(evt);
            }
        });

        jB_modifica.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jB_modifica.setText("Modificar");
        jB_modifica.setEnabled(false);
        jB_modifica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_modificaActionPerformed(evt);
            }
        });

        jB_elimina.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jB_elimina.setText("Eiminar");
        jB_elimina.setEnabled(false);
        jB_elimina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_eliminaActionPerformed(evt);
            }
        });

        jB_limpia.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jB_limpia.setText("Limpia Pantalla");
        jB_limpia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_limpiaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jT_buscanomina, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jB_modifica)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jB_elimina)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jB_limpia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 188, Short.MAX_VALUE)
                        .addComponent(jB_guarda)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jB_cancelar)
                        .addGap(20, 20, 20))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jB_cancelar)
                    .addComponent(jB_guarda)
                    .addComponent(jLabel14)
                    .addComponent(jT_buscanomina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jB_modifica)
                    .addComponent(jB_elimina)
                    .addComponent(jB_limpia))
                .addGap(3, 3, 3))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jB_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_cancelarActionPerformed
        int i=JOptionPane.showConfirmDialog(this,"Estas seguro de cancelar la operacion","Confirmacion",JOptionPane.YES_NO_OPTION);
        if(i==0){
        this.dispose();
        new login().setVisible(true);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jB_cancelarActionPerformed

    private void lb_fotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lb_fotoMouseClicked
        JFileChooser se=new JFileChooser();
        se.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int estado=se.showOpenDialog(null);
        if(estado==JFileChooser.APPROVE_OPTION){
            try{
            fis=new FileInputStream(se.getSelectedFile());
                //System.out.println(fis);
            this.longitudBytes=(int)se.getSelectedFile().length();
            Image icon=ImageIO.read(se.getSelectedFile()).getScaledInstance(lb_foto.getWidth(),lb_foto.getHeight(),Image.SCALE_DEFAULT);
            lb_foto.setIcon(new ImageIcon(icon));
            lb_foto.updateUI();
            }catch(FileNotFoundException ex){ex.printStackTrace();}
            catch(IOException ex){ex.printStackTrace();}
        }
        
        //TODO add your handling code here:
    }//GEN-LAST:event_lb_fotoMouseClicked

    private void jC_posicionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jC_posicionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jC_posicionActionPerformed

    private void jB_limpiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_limpiaActionPerformed
        LimpiaDatos();// TODO add your handling code here:
    }//GEN-LAST:event_jB_limpiaActionPerformed
    
    public String consultaNomina(String dato){
        String querfc = "select cveusuario from usuarios where cveusuario='" + dato + "'";
        String res1 = objCon.consultar(querfc, ServerBridge.getusuBD(), ServerBridge.getpasBD());
        String datos[] = res1.split(",");
        if(datos[0].equals(dato)){
        return "0";
        }else{
        return "1";
        }
        //return res1;
    }
    
    public void AddUsuario(){
    String queryAdd="insert into usuarios(cveusuario,nombre,password,"
            + "departamento,status,observaciones,fecha_alta,posicion,nivelEdu,uniclave,RYR,NYP,PDE,tipo_usuario,prin) values("
            + "'"+jT_nunomina.getText().trim()+"',"
            + "'"+rellenaCampos(jT_nombre.getText())+"',"
            + "'"+devuelvePass(jP_contra.getText()) +"',"
            + "'"+String.valueOf(jC_dpto.getSelectedItem()) +"',"
            + "'A',"
            + "'SIN OBSERVACIONES',"
            + "'"+fechaInsert(jF_fecha.getText()) +"',"
            + "'"+String.valueOf(jC_posicion.getSelectedItem()) +"',"
            + "'"+String.valueOf(jC_niveleducativo.getSelectedItem()) +"',"
            + "'"+jT_uniclave.getText()+"',"
            + "'"+rellenaCampos(jTA_RYR.getText())+"',"
            + "'"+rellenaCampos(jTA_NYP.getText())+"',"
            + "'"+rellenaCampos(jTA_PYE.getText())+"','"+jC_tipo.getSelectedItem()+"','"+jTA_PRIN.getText()+"')";
    String res = objCon.insertar(queryAdd, ServerBridge.getusuBD(), ServerBridge.getpasBD());
    if(res.equals("Exito!")){
        Cliente cl = new Cliente();
        cl.setNombre(jT_uniclave.getText());
        cl.setFoto(fis);
        
        //System.out.println(fis);
        //System.out.println(cl);
        int opcion = cc.insertCliente(cl);
        //System.out.println(opcion);
        if(opcion != 0)
         {
            try {
                //JOptionPane.showMessageDialog(this, "Registro "+cl+" agregado");
                System.out.println("Registro "+cl+" Agregado");
                if(fis != null)
                    fis.close();
                 fis = null;
            } catch (IOException ex) {
                Logger.getLogger(AM_registro.class.getName()).log(Level.SEVERE, null, ex);
            }
         }
    JOptionPane.showMessageDialog(this,"Se agrego el usuario exitosamente");
    }else{
    JOptionPane.showMessageDialog(this,"Ocurrio un error: "+res);
    }
            
    
    }
    
    public void LimpiaDatos(){
    jT_nombre.setText("");
    jT_nunomina.setText("");
    jP_contra.setText("");
    jT_uniclave.setText(objDatos.getCadenaAlfanumAleatoria(8));
    jC_posicion.setSelectedIndex(0);
    jC_dpto.setSelectedIndex(0);
    jC_niveleducativo.setSelectedIndex(0);
    jC_tipo.setSelectedIndex(0);
    jTA_NYP.setText("");
    jTA_PYE.setText("");
    jTA_RYR.setText("");
    lb_foto.setIcon(new CustomImageIcon(getClass().getResource("/resource/defaultlarge.gif")));
    jTA_PRIN.setText("");
    jF_fecha.setText(ServerBridge.getfechasistemacompleta());
    }
    
    
    public String devuelvePass(String dato){
    //String text=StringMD.getStringMessageDigest(dato,"SHA-256");
        String text=c.encriptar(dato);
    return text;
    }
    
    
    public String fechaInsert(String dato){
    String datos[] = dato.split("\\/");
    return (datos[2]+"-"+datos[1]+"-"+datos[0]+" 12:00:000");
    }
    
    private void jB_guardaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_guardaActionPerformed
        if(jT_nunomina.getText().equals("")||jP_contra.getText().equals("")||jF_fecha.getText().equals("  /  /    ")){
        JOptionPane.showMessageDialog(this,"Es necesario por lo menos ingresar No. Nomina,Contraseña y Fecha");
        }else{
            if(consultaNomina(jT_nunomina.getText()).equals("0")){
            JOptionPane.showMessageDialog(this,"El numero de nomina: "+jT_nunomina.getText()+" Ya esta dado de alta");
            }else{
                //System.out.println("SE DARA DE ALTA");
                AddUsuario();
                LimpiaDatos();
                lb_foto.setIcon(new CustomImageIcon(getClass().getResource("/resource/defaultlarge.gif")));
            }
        
        }// TODO add your handling code here:
    }//GEN-LAST:event_jB_guardaActionPerformed
    
    
    public String rellenaCampos(String dato){
    if(dato.equals("")){
    return "SIN REGISTRO";
    }else{
    return dato;
    }
    
    }
    
    private void jT_buscanominaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jT_buscanominaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            evt.setKeyCode(0);
            String querfc = "select cveusuario from usuarios where cveusuario='" + jT_buscanomina.getText() + "' and status='A'";
            String res1 = objCon.consultar(querfc, ServerBridge.getusuBD(), ServerBridge.getpasBD());
            String datos[] = res1.split(",");
            if(datos[0].equals(jT_buscanomina.getText())){
                if(consultarPsw(jT_buscanomina.getText())){
                    //System.out.println("CONSULTA DATOS");
                    String queryDat = "select * from usuarios where cveusuario='" + jT_buscanomina.getText() + "'";
                    String resdat = objCon.consultarB(queryDat, ServerBridge.getusuBD(), ServerBridge.getpasBD());
                    String datosdat[] = resdat.split("\\¬");
                    System.out.println(queryDat);
                    jT_nombre.setText(datosdat[2]);
                    jT_nunomina.setText(datosdat[1]);
                    jP_contra.setText(c.desencriptar(datosdat[3]));
                    System.out.println(c.desencriptar(datosdat[3]));
                    jT_uniclave.setText(datosdat[10]);
                    jC_posicion.setSelectedItem(datosdat[8]);
                    jC_dpto.setSelectedItem(datosdat[4]);
                    jC_niveleducativo.setSelectedItem(datosdat[9]);
                    jTA_NYP.setText(datosdat[12]);
                    jTA_PYE.setText(datosdat[13]);
                    jTA_RYR.setText(datosdat[11]);
                    jC_tipo.setSelectedItem(datosdat[14]);
                    jF_fecha.setText(cargafecha(datosdat[7]));
                    jTA_PRIN.setText(datosdat[15]);
                    CustomImageIcon foto = ConexionBase.getFoto(datosdat[10]);
                    ImageIcon fot=ConexionBase.getFoto(datosdat[10]);
                    //System.out.println(foto);
                    if(fot!=null)
                    {   
                        Icon icono = new ImageIcon(fot.getImage().getScaledInstance(lb_foto.getWidth(), lb_foto.getHeight(), Image.SCALE_DEFAULT));
                    
                        lb_foto.setIcon(icono);
                    }else{
                        
                     lb_foto.setIcon(new CustomImageIcon(getClass().getResource("/resource/defaultlarge.gif")));
                    }
                    lb_foto.updateUI();
                    
                    jB_modifica.setEnabled(true);
                    jB_elimina.setEnabled(true);
                    jB_limpia.setEnabled(false);
                    jB_guarda.setEnabled(false);
                }else{
                    JOptionPane.showMessageDialog(this,"La contraseña es incorrecta");
                }
            }else{
            JOptionPane.showMessageDialog(this,"No existe el numero de nomina introducido o ya esta dado de baja");
            }
        }// TODO add your handling code here:
    }//GEN-LAST:event_jT_buscanominaKeyPressed
    
    
    public String cargafecha(String dato){
    String datos[] = dato.split("\\s");
    String datos1[] = datos[0].split("\\-");
    return datos1[2]+"/"+datos1[1]+"/"+datos1[0];
    }
    
    
    private ImageIcon ajustarImagen(String ico)
    {
        ImageIcon tmpIconAux = new ImageIcon(ico);
        //Escalar Imagen
        ImageIcon tmpIcon = new ImageIcon(tmpIconAux.getImage().getScaledInstance(200, 200, Image.SCALE_DEFAULT));
        return tmpIcon;
    }
    
    private void jB_modificaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_modificaActionPerformed
        if(jT_nunomina.getText().equals("")||jP_contra.getText().equals("")){
        JOptionPane.showMessageDialog(this,"Es necesario por lo menos ingresar No. Nomina y Contraseña");
        }else{
            System.out.println("MODIFICAR DATOS");
            jB_modifica.setEnabled(false);
            jB_elimina.setEnabled(false);
            jB_limpia.setEnabled(true);
            jB_guarda.setEnabled(true);
        }// TODO add your handling code here:
    }//GEN-LAST:event_jB_modificaActionPerformed

    private void jB_eliminaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_eliminaActionPerformed
        int i=JOptionPane.showConfirmDialog(this,"Estas seguro de eliminar al usuario","Confirmacion",JOptionPane.YES_NO_OPTION);
        if(i==0){
        deleteUsu(jT_uniclave.getText());
        }//if(){}else{}// TODO add your handling code here:
    }//GEN-LAST:event_jB_eliminaActionPerformed
    
    public void deleteUsu(String dato){
        String queDel = "update usuarios set "
                    + "status='D' where uniclave='" + dato + "'";
            String res = objCon.modificar(queDel, ServerBridge.getusuBD(), ServerBridge.getpasBD());
            if (res.equals("Exito!")) {
                JOptionPane.showMessageDialog(this, "Se elimino correctamente al cliente");
                LimpiaDatos();
                lb_foto.setIcon(new CustomImageIcon(getClass().getResource("/resource/defaultlarge.gif")));
            
                
            } else {
                JOptionPane.showMessageDialog(this, "Ocurrio un error al eliminar el registro " + res);
            }
    }
    
    
    public boolean consultarPsw(String dato) {
        String query = "select password from usuarios where cveusuario = '" + dato + "'";
        String res = objCon.consultar(query, ServerBridge.getusuBD(), ServerBridge.getpasBD());
        String datos[] = res.split(",");
        String password=c.desencriptar(datos[0]);
        //System.out.println(datos[0]);
        //System.out.println(password);
        //String password = res.substring(0, res.length() - 1);
        //String passText = StringMD.getStringMessageDigest(jPasswordFieldJoin.getText(), "SHA-256");
        //JOptionPane.showMessageDialog(this,passText);        
        //System.out.println(password);
        //System.out.println(passText);
        if (password.equals(selectPassword())) {
            return true;
        } else {
            return false;
        }
    }

    private String selectPassword() {
        JPasswordField pwd = new JPasswordField();
        String userPass = "";
        Object[] message = {"Contraseña del usuario:  \n", pwd};
        int resp = JOptionPane.showConfirmDialog(null, message, "Retrieve Password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (resp == JOptionPane.OK_OPTION) {
            //userPass = new String(StringMD.getStringMessageDigest(pwd.getText(), "SHA-256"));
            userPass=pwd.getText();
        }
        return userPass;
    }
    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jB_cancelar;
    private javax.swing.JButton jB_elimina;
    private javax.swing.JButton jB_guarda;
    private javax.swing.JButton jB_limpia;
    private javax.swing.JButton jB_modifica;
    private javax.swing.JComboBox jC_dpto;
    private javax.swing.JComboBox jC_niveleducativo;
    private javax.swing.JComboBox jC_posicion;
    private javax.swing.JComboBox jC_tipo;
    private javax.swing.JFormattedTextField jF_fecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField jP_contra;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jTA_NYP;
    private javax.swing.JTextArea jTA_PRIN;
    private javax.swing.JTextArea jTA_PYE;
    private javax.swing.JTextArea jTA_RYR;
    private javax.swing.JTextField jT_buscanomina;
    private javax.swing.JTextField jT_nombre;
    private javax.swing.JTextField jT_nunomina;
    private javax.swing.JTextField jT_uniclave;
    private javax.swing.JLabel lb_foto;
    // End of variables declaration//GEN-END:variables
}
